package com.tt.controller;

import com.tt.common.page.TtResult;
import com.tt.pojo.GoodsCat;
import com.tt.pojo.vo.CatVo;
import com.tt.service.IGoodsCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类管理
 * @author 青青
 */
@Controller
@RequestMapping("/cat")
public class CategoryController
{
    private String prefix = "cat";

    @Autowired
    private IGoodsCatService goodsCatService;

    /**
     * 商品分类页面
     * @return
     */
    @GetMapping("/catPage")
    public String catPage(){
        return prefix + "/cat";
    }


    /**
     * 获取分类 如果不传pid 默认为 0
     * @param id
     * @return
     */
    @RequestMapping("/getGoodsCatList")
    @ResponseBody
    public List<GoodsCat> getGoodsCatList( @RequestParam(required = false,defaultValue = "0") Integer id)
    {
        List<GoodsCat> catList = goodsCatService.getCatList(id);
        return catList;
    }

    @GetMapping("/getGoodsCat")
    @ResponseBody
    public TtResult getGoodsCat(@RequestParam("id") Integer id)
    {
        GoodsCat goodsCatByID = goodsCatService.getGoodsCatByID(id);
        return TtResult.ok(goodsCatByID);
    }

    @PostMapping("/getCatVo")
    @ResponseBody
    public CatVo getCatVo(@RequestParam("id") Integer id)
    {
        CatVo catVo = goodsCatService.getCatVo(id);
        return catVo;
    }


    @PostMapping("/updateGoodsCat")
    @ResponseBody
    public TtResult updateGoodsCat(GoodsCat goodsCat){
        int i = goodsCatService.updateGoodsCat(goodsCat);
        return TtResult.ok(i);
    }

    @PostMapping("/delGoodsCat")
    @ResponseBody
    public TtResult delGoodsCat(@RequestParam("id") Integer id){
        int i = goodsCatService.delGoodsCat(id);
        return TtResult.ok(i);
    }

}
